﻿using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace VH.SharePoint.Api.RSD
{
    [XmlRoot("rsd", Namespace = "http://archipelago.phrasewise.com/rsd" ,IsNullable = true)]
    public class Rsd
    {
        [XmlAttribute("version")]
        public String Version { get; set; }
        [XmlElement("service")]
        public Service Service { get; set; }

        public Rsd()
        {
            Version = "1.0";
            Service = new Service();
        }

        public override string ToString()
        {
            return ToXml();
        }

        public String ToXml()
        {
            var xmls = new XmlSerializer(this.GetType());
            using (var ms = new MemoryStream())
            {
                var settings = new XmlWriterSettings();
                settings.Encoding = Encoding.UTF8;
                settings.Indent = true;
                settings.IndentChars = "\t";
                settings.NewLineChars = Environment.NewLine;
                settings.ConformanceLevel = ConformanceLevel.Document;

                var ns = new XmlSerializerNamespaces();
                ns.Add("", "http://archipelago.phrasewise.com/rsd");

                using (var writer = XmlTextWriter.Create(ms, settings))
                {
                    xmls.Serialize(writer, this, ns);
                }

                var xml = Encoding.UTF8.GetString(ms.ToArray());
                return xml;
            }
        }
    }
}